package com.niu.wm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.niu.wm.service.BaseService;
import com.niu.wm.service.CategoryService;
import com.niu.wm.core.error.BizException;
import com.niu.wm.core.error.GlobalErrorCode;
import com.niu.wm.dao.mapper.ProductCategoryMapper;
import com.niu.wm.model.ProductCategory;
import com.niu.wm.model.User;

@Service
public class CategoryServiceImpl extends BaseService implements CategoryService {
	private Map<Long,ProductCategory> cacheById = null;
	private Map<String, ProductCategory> cacheByCode = null;
	
    @Autowired
    private ProductCategoryMapper  categoryMapper;
	
    @Override
   	public void refrechCache(){
    	cacheById = null;
    	cacheByCode = null;
    }
    
    private void refreshCache(Long id, String code){
    	if( cacheById == null){
    		cacheById = new HashMap<Long, ProductCategory>();
    		cacheByCode = new HashMap<String, ProductCategory>();
    	}
    	if(id==null && StringUtils.isBlank(code))
    		return;
    	
    	ProductCategory category = null;
    	if(null != id )
    		category = categoryMapper.selectByPrimaryKey(id);
    	else 
    		category = categoryMapper.selectByCode(code);
    	
    	if(category == null)
    		this.getLogger().error("dict not found id=" + id + " code=" + code);
    	
    	if( category != null){
    		cacheById.put(category.getId(), category);
    		cacheByCode.put(category.getCode(), category);
    	}
    }
    
    @Override
	public ProductCategory load(Long id){
		return load(id,null);
	}
    
    @Override
	public ProductCategory loadByCode(String code){
    	return load(null, code);
	}
    
    private ProductCategory load(Long id, String code){
    	ProductCategory category = null;
    	if( null != cacheById ){
    		if(null != id )
    			category = cacheById.get(id);
        	else 
        		category = cacheByCode.get(code); 
    	}
    	
    	if(category == null){
    		refreshCache(id, code);
    		if(null != id )
    			category = cacheById.get(id);
        	else 
        		category = cacheByCode.get(code);  
    	}
    	
    	return category;
    }
    
    @Override
	public int add(User user, ProductCategory category){    	
		return categoryMapper.insert(category);
	}
    
    @Override
	public int delete(User user, Long id){
		return categoryMapper.delete(id);
	}
    
    @Override
	public int update(User user, ProductCategory dict){
		int count = categoryMapper.update(dict);	
		
		return count;
	}
    
    @Override
	public List<ProductCategory> list(){
    	return categoryMapper.list();
    }
}
